package com.all.base;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import com.android.AndroidApplication;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.Window;

public abstract class BaseActivity extends Activity implements OnClickListener {

	protected Context mContext = null;
	/**日志变量，获取类的名称**/
	protected final String TAG = this.getClass().getSimpleName();
	//通过AndroidApplication来对activity进行管理
	AndroidApplication application = null;
	
	private WeakReference<Activity> mWeakReference = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "BaseActivity-->onCreate()");
		//初始化Application
		application = (AndroidApplication)getApplication();
		//对当前activity进行软引用
		mWeakReference = new WeakReference<Activity>(this);
		//将当前Activity压入栈
		application.pushTask(mWeakReference);
		mContext = this;
		
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		
		Log.d(TAG, "BaseActivity-->onRestart()");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "BaseActivity-->onStart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "BaseActivity-->onResume()");
		//resume();
	}


	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "BaseActivity-->onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "BaseActivity-->onStop()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "BaseActivity-->onDestroy()");
		
		//destroy();
		//mApplication.removeTask(context);
	}
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
			if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
				try {
					Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
					m.setAccessible(true);
					m.invoke(menu, true);
				} catch (Exception e) {
					Log.e(TAG, "onMenuOpened-->"+e.getMessage());
				}
			}
		}
		return super.onMenuOpened(featureId, menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	/**
	 * 获取当前Activity
	 * @return
	 */
	protected Activity getContext(){
		if(null != mWeakReference)
			return mWeakReference.get();
		else
			return null;
	}

}
